package com.nbsaas.boot.evaluate.ext.resource;

import com.nbsaas.boot.evaluate.api.apis.EvaluateApi;
import com.nbsaas.boot.evaluate.api.apis.EvaluateFileApi;
import com.nbsaas.boot.evaluate.api.domain.request.EvaluateFileRequest;
import com.nbsaas.boot.evaluate.api.domain.response.EvaluateResponse;
import com.nbsaas.boot.evaluate.ext.apis.EvaluateExtApi;
import com.nbsaas.boot.evaluate.ext.domain.request.EvaluateExtRequest;
import com.nbsaas.boot.rest.response.ResponseObject;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class EvaluateExtResource implements EvaluateExtApi {

    @Resource
    private EvaluateApi evaluateApi;

    @Resource
    private EvaluateFileApi evaluateFileApi;

    @Transactional
    @Override
    public ResponseObject<?> createExt(EvaluateExtRequest request) {
        ResponseObject<EvaluateResponse> res = evaluateApi.create(request);
        if (res.getCode() != 200) {
            return res;
        }
        if (request.getFiles() != null) {
            for (EvaluateFileRequest file : request.getFiles()) {
                file.setEvaluate(res.getData().getId());
                evaluateFileApi.create(file);
            }
        }
        return res;
    }
}
